/**
 * Created by mac on 2017/6/2.
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import java.io.*;
import java.security.*;
import java.util.Arrays;
import javax.crypto.*;

public class Client3 {
    // 搭建客户端
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        MyBC mybc=new MyBC();
        try {
            Socket socket = new Socket("192.168.43.252",5204);
            System.out.println("客户端启动成功");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter write = new PrintWriter(socket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String readline;
            System.out.println("Client:" );
            readline = br.readLine();
            String line=mybc.evaluate(readline);
            FileInputStream f=new FileInputStream("key1.dat");
            ObjectInputStream b=new ObjectInputStream(f);
            Key k=(Key)b.readObject();
            Cipher cp=Cipher.getInstance("DESede");
            cp.init(Cipher.ENCRYPT_MODE, k);
            byte ptext[]=line.getBytes("UTF-8");


            byte ctext[]=cp.doFinal(ptext);
            String Str=new String(ctext,"ISO-8859-1");

            write.println(Str);
            write.flush();

            System.out.println("Server return:" + in.readLine());

            write.close();
            in.close();
            socket.close();

        } catch (Exception e) {
            System.out.println("can not listen to:" + e);
        }

    }

}
